package com.zxbg.controller.systemtoolsController;

import com.zxbg.controller.Code;
import com.zxbg.controller.Result;
import com.zxbg.domain.Employee;
import com.zxbg.service.systemtoolsService.OrganizationalStructureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/OrganizationalStructure")
public class OrganizationalStructureController {
    @Autowired
    OrganizationalStructureService organizationalStructureService;

    @GetMapping("/getRootNode")
    public Result getRootNode(@RequestHeader Integer tenant_id) {
        return new Result(Code.GET_OK, organizationalStructureService.getRootNode(tenant_id));
    }

    @GetMapping("/{dept_id}")
    public Result getDepartmentByParentDeptId(@PathVariable Integer dept_id, @RequestHeader Integer tenant_id) {
        return new Result(Code.GET_OK, organizationalStructureService.getDepartmentByParentDeptId(dept_id, tenant_id));
    }

    @PostMapping
    public Result getEmployeesByWorkerId(@RequestBody Map<String, Object> search_info, @RequestHeader Integer tenant_id) {
        if(search_info.get("worker_id") == null || search_info.get("worker_id").equals("")) {
            if(search_info.get("current_page") != null && search_info.get("page_size") != null) {
                return getPageResult(new ArrayList<>(), search_info);
            } else {
                return new Result(Code.GET_OK, new ArrayList<>());
            }
        }
        List<Employee> employeeList = organizationalStructureService.getEmployeesByWorkerId(search_info, tenant_id);
        if(search_info.get("current_page") != null && search_info.get("page_size") != null) {
            List<Employee> resList = new ArrayList<>();
            Integer startNum = ((Integer)search_info.get("current_page") - 1) * (Integer)search_info.get("page_size");
            Integer endNum = (Integer)search_info.get("current_page") * (Integer)search_info.get("page_size") - 1;
            for (int i = 0; i < employeeList.size(); i++) {
                if(i >= startNum && i <= endNum) {
                    resList.add(employeeList.get(i));
                }
            }
            return getPageResult(resList, search_info);
        } else {
            return new Result(Code.GET_OK, employeeList);
        }
    }

    public Result getPageResult(List<Employee> employeeList, Map<String, Object> search_info) {
        Map<String,Object> first_map = new HashMap<>();
        Map<String,Object> second_map = new HashMap<>();
        second_map.put("page_size", search_info.get("page_size"));
        second_map.put("current_page", search_info.get("current_page"));
        second_map.put("total", employeeList.size());
        first_map.put("page", second_map);
        first_map.put("rows", employeeList);
        return new Result(Code.GET_OK, first_map);
    }

    @PostMapping("/addEmployee")
    public Result addEmployee(@RequestBody Employee employee, @RequestHeader Integer tenant_id) {
        String flag = organizationalStructureService.addEmployee(employee, tenant_id);
        return new Result(flag.length() == 0 ? Code.SAVE_OK : Code.SAVE_ERR, flag.length() == 0 ? "success" : "error", flag);
    }

    @PutMapping
    public Result updateEmployee(@RequestBody Employee employee, @RequestHeader Integer tenant_id) {
        String flag = organizationalStructureService.updateEmployee(employee, tenant_id);
        return new Result(flag.length() == 0 ? Code.UPDATE_OK : Code.UPDATE_ERR, flag.length() == 0 ? "success" : "error", flag);
    }

    @PutMapping("/deactivateEmployee")
    public Result deactivateEmployee(@RequestBody List<String> employee_ids, @RequestHeader Integer tenant_id){
        boolean flag = organizationalStructureService.changeEmployeeStatus(employee_ids, tenant_id, 0);
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag);
    }

    @PutMapping("/restoreEmployee")
    public Result restoreEmployee(@RequestBody List<String> employee_ids, @RequestHeader Integer tenant_id){
        boolean flag = organizationalStructureService.changeEmployeeStatus(employee_ids, tenant_id, 1);
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag);
    }

    @PutMapping("/{employee_id}")
    public Result changePassword(@PathVariable String employee_id, @RequestBody String newPassword, @RequestHeader Integer tenant_id) {
        boolean flag = organizationalStructureService.changePassword(employee_id, newPassword, tenant_id);
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag);
    }

    @PostMapping("/addSubDept")
    public Result addSubDept(@RequestBody Map<String, String> map, @RequestHeader Integer tenant_id) {
        String info = organizationalStructureService.addSubDept(Integer.parseInt(map.get("dept_id")), map.get("add_dept"), tenant_id);
        return new Result(info.equals("") ? Code.SAVE_OK : Code.SAVE_ERR, info, info);
    }

    @PutMapping("/changeDeptName")
    public Result changeDeptName(@RequestBody Map<String, String> map) {
        String info = organizationalStructureService.changeDeptName(Integer.parseInt(map.get("dept_id")), map.get("new_name"));
        return new Result(info.equals("") ? Code.UPDATE_OK : Code.UPDATE_ERR, info, info);
    }

    @DeleteMapping("/{dept_id}")
    public Result deleteDept(@PathVariable Integer dept_id) {
        return new Result(Code.DELETE_OK, organizationalStructureService.deleteDept(dept_id));
    }

    @PutMapping("deptMove")
    public Result deptMove(@RequestBody Map<String, Integer> map) {
        return new Result(Code.UPDATE_OK, organizationalStructureService.deptMove(map.get("dept_id"), map.get("direction")));
    }

    @PostMapping("/setDeptHeader/{dept_id}")
    public Result setDeptHeader(@RequestBody List<Employee> employeeList, @PathVariable Integer dept_id, @RequestHeader Integer tenant_id) {
        return new Result(Code.SAVE_OK, organizationalStructureService.setDeptHeader(employeeList, dept_id, tenant_id));
    }

    @PostMapping("/searchDept")
    public Result searchDept(@RequestBody String key, @RequestHeader Integer tenant_id) {
        return new Result(Code.GET_OK, organizationalStructureService.searchDept(key, tenant_id));
    }
}
